<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { userUpdatePasswordService } from '@/api/user.js';

const passwordData = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: '',
});
const checkRePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次确认密码'));
  } else if (value !== passwordData.value.new_pwd) {
    callback(new Error('请确保两次输入的密码一样'));
  } else {
    callback();
  }
};
const rules = {
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    {
      pattern: /^\S{5,16}$/,
      message: '密码必须是5-16位的非空字符串',
      trigger: 'blur',
    },
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{5,16}$/,
      message: '密码必须是5-16位的非空字符串',
      trigger: 'blur',
    },
  ],
  re_pwd: [
    { validator: checkRePassword, trigger: 'blur' }
  ]
};

const updatePassword = async () => {


  try {
    const response = await userUpdatePasswordService(passwordData.value);
    if (response.code === 0) {
      ElMessage.success(response.msg || '密码更新成功');
    } else {
      ElMessage.error(response.msg || '密码更新失败');
    }
  } catch (error) {
    ElMessage.error('请求失败，1请重试');
  }
};
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>重置密码</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form :model="passwordData" :rules="rules" label-width="100px" size="large">
          <el-form-item label="原密码" prop="old_pwd">
            <el-input v-model="passwordData.old_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input v-model="passwordData.new_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="re_pwd">
            <el-input v-model="passwordData.re_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updatePassword">提交修改</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>
